package com.sunzm.common.utils;

import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 文件操作工具类
 *
 * @author Administrator
 * @version 1.0
 * @date 2021-06-10 18:53
 */
public class FileOpeUtils {
    public static void zipFile(File srcFile, File zipedFile) throws Exception {
        ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(zipedFile);

        zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded);

        ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(srcFile, srcFile.getName());

        zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);

        InputStream inputStream = new FileInputStream(srcFile);
        IOUtils.copy(inputStream, zipArchiveOutputStream);

        zipArchiveOutputStream.closeArchiveEntry();
        zipArchiveOutputStream.finish();

        //关闭输入流
        if (null != inputStream) {
            inputStream.close();
        }
        //关闭输出流
        if (null != zipArchiveOutputStream) {
            zipArchiveOutputStream.close();
        }
    }
}
